package com.topisv.tms.repository.lpn;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.lpn.Pallet;

public interface PalletDao extends PagingAndSortingRepository<Pallet, Long>, JpaSpecificationExecutor<Pallet> {
    
    @Modifying
    @Query("UPDATE Pallet SET status = ?1 WHERE id in ?2  ")
    int modifyStatusByIds(int status, List<Long> ids);
    
    @Modifying
    @Query("UPDATE Pallet SET status = ?1 WHERE lpnNo in ?2  ")
    int modifyStatusByLpns(int status, List<String> lpns);
    
    @Modifying
    @Query("UPDATE Pallet SET status = ?1 WHERE lpnNo = ?2  ")
    int modifyStatusByLpn(int status, String lpn);
    
    @Query("SELECT p.palletNum FROM Pallet p where p.warehouseCode = ?1")
    int getMaxPalletNumWhc(String warehouseCode);
    
    @Query("SELECT P FROM Pallet P WHERE P.lpnNo = ?")
    Pallet getPalletByLpn(String lpn);
    
    @Modifying
    @Query("UPDATE Pallet SET status = ?1 WHERE id = ?2 and lpnNo = ?3 and status = ?4 ")
    int loadedPalletByIdAndLpn(int status, long id, String lpn, int oldStatus);
    
    @Modifying
    @Query("UPDATE Pallet SET status = ?1 WHERE (id = ?2 or lpnNo = ?3) and status = ?4 ")
    int loadedPalletByIdOrLpn(int status, long id, String lpn, int oldStatus);
    
    @Modifying
    @Query("UPDATE Pallet SET status = ?1 WHERE id = ?2 and status = ?3 ")
    int loadedPalletById(int status, long id, int oldStatus);
    
    @Modifying
    @Query("UPDATE Pallet SET status = ?1 WHERE lpnNo = ?2 and status = ?3 ")
    int loadedPalletByLpn(int status, String lpn, int oldStatus);
    
    @Query("SELECT p FROM Pallet p WHERE p.lpnNo = ?1 and p.status = ?2 ")
    List<Pallet> existsByLpnAndStatus(String lpn, int status);
    
}
